package com.lzx.study.Controller;

import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;

@RestController
public class StreamingOutputController {
    private static final Logger log = LoggerFactory.getLogger(StreamingOutputController.class);
    @Resource(name = "deepseek")
    private ChatModel deepseekChatModel;
    @Resource(name = "qwen")
    private ChatModel qwenChatModel;
    @Resource(name = "deepseekChatClient")
    private ChatClient deepseekChatClient;
    @Resource(name = "qwenChatClient")
    private ChatClient qwenChatClient;

    @Value("classpath:/prompttemplate/lzx-template.txt")
    private org.springframework.core.io.Resource userTemplate;


    @GetMapping("/stream/chatflux1")
    public Flux<String> chatFlux1(@RequestParam(name = "question", defaultValue = "你是谁") String question) {
        return deepseekChatModel.stream(question);
    }
    @GetMapping("/stream/chatflux2")
    public Flux<String> chatFlux2(@RequestParam(name = "question", defaultValue = "你是谁") String question) {
        return qwenChatModel.stream(question);
    }
    @GetMapping("/stream/chatflux3")
    public Flux<String> chatFlux3(@RequestParam(name = "question", defaultValue = "你是谁") String question) {
        return deepseekChatClient.prompt(question).stream().content();
    }
    @GetMapping("/stream/chatflux4")
    public Flux<String> chatFlux4(@RequestParam(name = "question", defaultValue = "你是谁") String question) {
        return qwenChatClient.prompt(question).stream().content();
    }
    @GetMapping("/stream/chatflux5")
    public Flux<String> chatFlux5(@RequestParam(name = "question", defaultValue = "你是谁") String question) {
        UserMessage userMessage = new UserMessage(question);

        return deepseekChatModel.stream(userMessage);
    }
    @GetMapping("/stream/chatflux6")
    public Flux<String> chatFlux6(@RequestParam(name = "question", defaultValue = "介绍一下你们的学校") String question) {
        return deepseekChatClient.prompt()
                .system("你是一个叫罗旺的学生，只能回答江西财经大学相关的知识")
                .user(question)
                .stream()
                .content();
    }
//   1.简单使用提示词模板
    @GetMapping("/prompttemplate/chat")
    public Flux<String> chatFlux7(String topic,String output_format,String wordCount) {
        PromptTemplate promptTemplate = new PromptTemplate(
                "" +
                "讲一个关于{topic}的故事" +
                "并以{output_format}格式输出," +
                "字数控制在{wordCount}字以内"
        );
        Prompt prompt = promptTemplate.create(Map.of(
                "topic", topic,
                "output_format", output_format,
                "wordCount", wordCount
        ));
        Flux<String> content = deepseekChatClient.prompt(prompt)
                .stream()
                .content();
        log.info("{}", content);
        return content;
    }
//    2.读取提示词模板文件
    @GetMapping("/prompttemplate/chat2")
    public Flux<String> chatFlux8(String topic,String output_format){
        PromptTemplate promptTemplate = new PromptTemplate(userTemplate);
        Prompt prompt = promptTemplate.create(Map.of(
                "topic", topic,
                "output_format", output_format
        ));
        Flux<String> content = deepseekChatClient.prompt(prompt)
                .stream()
                .content();
        log.info("{}", content);
        return content;
    }
//    3.角色设定和边界划分
@GetMapping("/prompttemplate/chat3")
public Flux<String> chatFlux9(String sysTopic,String userTopic){
    SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate("你是{systemTopic}助手，只回答{systemTopic}相关的问题");
    Message sysMessage = systemPromptTemplate.createMessage(Map.of("systemTopic", sysTopic));
    PromptTemplate userPromptTemplate = new PromptTemplate("解释一下{userTopic}，以html格式返回");
    Message userMessage = userPromptTemplate.createMessage(Map.of("userTopic", userTopic));
    Prompt prompt = new Prompt(List.of(sysMessage, userMessage));
    Flux<String> content = deepseekChatClient.prompt(prompt)
            .stream()
            .content();
    log.info("{}", content);
    return content;
}

}
